package com.example.imchat.controller.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.example.imchat.R;
import com.example.imchat.controller.adapter.GroupListAdapter;
import com.example.imchat.model.Model;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.exceptions.HyphenateException;

import java.util.ArrayList;
import java.util.List;

public class GroupListActivity extends Activity {
    private ListView lv_grouplist;
    private  GroupListAdapter groupListAdapter;
    private LinearLayout ll_grouplist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group);

        //初始化控件
        initView();
        initData();

        initListener();
    }

    private void initListener() {
        //群聊listview条目点击事件   点击进入群聊界面
        lv_grouplist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position ==0 ){
                    //头布局问题
                    return;
                }
                Intent intent = new Intent(GroupListActivity.this, ChatActivity.class);
                //传递参数  类型
                intent.putExtra(EaseConstant.EXTRA_CHAT_TYPE,EaseConstant.CHATTYPE_GROUP);
                //群id
                EMGroup emGroup = EMClient.getInstance().groupManager().getAllGroups().get(position - 1);
                intent.putExtra(EaseConstant.EXTRA_USER_ID,emGroup.getGroupId());


                startActivity(intent);
            }
        });
        ll_grouplist.setOnClickListener(new View.OnClickListener() {
            //创建群聊的点击事件
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GroupListActivity.this, NewGroupActivity.class);
                startActivity(intent);

            }
        });

    }

    private void initData() {
        groupListAdapter = new GroupListAdapter(this);
        lv_grouplist.setAdapter(groupListAdapter);

        //从环信服务器获取所有群组
        getGroupFromServer();

    }

    private void getGroupFromServer() {
        Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    //从网络获取群组信息
                    List<EMGroup> mGroups = EMClient.getInstance().groupManager().getJoinedGroupsFromServer();

                    //更新页面
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(GroupListActivity.this,"加载群信息成功",Toast.LENGTH_SHORT).show();
                            refresh();
                        }
                    });


                } catch (HyphenateException e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(GroupListActivity.this,"加载群信息失败",Toast.LENGTH_SHORT).show();
                            refresh();
                        }
                    });

                }
            }
        });
    }
    //刷新
    private void refresh(){
        groupListAdapter.refresh(EMClient.getInstance().groupManager().getAllGroups());

    }
    private void initView() {
        lv_grouplist = (ListView)findViewById(R.id.lv_grouplist);
        //添加头布局
        View headerview = View.inflate(this,R.layout.header_group,null);
        lv_grouplist.addHeaderView(headerview);
        ll_grouplist = headerview.findViewById(R.id.ll_grouplist);

    }

    @Override
    protected void onResume() {
        super.onResume();
        refresh();
    }
}
